package com.util.database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DatabaseManager {
    public static Connection connection;
    public static final String DRIVER = "com.mysql.cj.jdbc.Driver";
    public static final String USERNAME = "developer";
    public static final String PASSWORD = "bjtuasc@2022";
    public static final String URL = "jdbc:mysql://rm-m5e1ezsmj440505yuoo.mysql.rds.aliyuncs.com:3306/oa?autoReconnect=true&useSSL=false";

    // establish connection
    public DatabaseManager() throws ClassNotFoundException, SQLException {
        //Registering with mysql Driver
        Class.forName(DRIVER);
        connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
    }

    // close connection
    public void close() throws SQLException {
        if(connection != null){
            connection.close();
            connection = null;
        }
    }

    public ResultSet executeQuery(String sql) throws SQLException {
        return connection.createStatement().executeQuery(sql);
    }

    public void executeUpdate(String sql) throws SQLException {
        connection.createStatement().executeUpdate(sql);
    }

    public boolean execute(String sql) throws SQLException {
        return connection.createStatement().execute(sql);
    }

}
